<?php

/**
* 
*/
class api extends Module
{
	public function index()
	{
		$this->res->text = 'Yes '.$_SESSION['name'].'! you\r connected!';
	}
	public function login($user='',$pass='')
	{
		$res = $this->db->prepare('SELECT u.dni, COUNT(u.dni) num, u.name FROM users u WHERE u.dni = ? AND u.pass = ? LIMIT 1',array($user,sha1($pass)));

		$this->res->type = 'response';
		if($res[0]['num'] > 0)
		{
			$hash = sha1($res[0]['name'].$user.microtime(TRUE));
			$update = $this->db->prepare('UPDATE users u SET u.hash = ? WHERE u.dni = ? AND u.pass = ? ',array($hash,$user,sha1($pass)),FALSE);
			
			$this->res->stat = 'OK';
			$this->res->data = array(new stdClass);
			$this->res->data[0]->name = $res[0]['name'];
			$this->res->data[0]->hash = $hash;
		}
		else
		{
			$this->res->stat = 'FAIL';
			$this->res->data = array();
		}
	}
	public function accounts()
	{
		$res = $this->db->prepare('SELECT * FROM rel_accounts ra WHERE ra.user = ?',array($this->hash));
		$this->res->type = 'response';
		$this->res->stat = 'OK';
		$this->res->data = $res;
		
	}
	public function category($type = 'category')
	{
		if($type !== 'category')
			$type = ($type === 'subcategory') ? 'subcategory' : 'category';
		
		$res = $this->db->prepare('SELECT * FROM '.$type.' c');
		$this->res->type = 'response';
		$this->res->stat = 'OK';
		$this->res->data = $res;
	}
}


?>